package br.com.vigilantebsb.bean;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.map.Circle;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import br.com.arquitetura.excecao.ExcecaoUtil;
import br.com.arquitetura.service.UniversalManager;
import br.com.arquitetura.util.ConstantesARQ;
import br.com.arquitetura.util.FacesMessagesUtil;
import br.com.vigilantebsb.entidade.Ocorrencia;
import br.com.vigilantebsb.util.CustomCircle;

import com.google.code.geocoder.Geocoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderRequest;
import com.google.code.geocoder.model.GeocoderResult;

@ManagedBean(name = "principalBean")
@ViewScoped
public class PrincipalBean implements Serializable {

	private static final long serialVersionUID = -7355462417028175561L;

	private String local;
	private String coordenadas;
	private List<SelectItem> listaLocais;
	private Ocorrencia ocorrencia = new Ocorrencia();
	private Ocorrencia ocorrenciaVisu = new Ocorrencia();

	@ManagedProperty(value = "#{universalManager}")
	protected UniversalManager universalManager;

	private MapModel circleModel;

	public void carregarCirulos() {
		circleModel = new DefaultMapModel();

		List<Ocorrencia> ocorrencias = universalManager.getAll(Ocorrencia.class);
		for (Ocorrencia list : ocorrencias) {
			double latitude = Double.valueOf(list.getLatitude().replaceAll(",", "."));
			double longitude = Double.valueOf(list.getLongitude().replaceAll(",", "."));
			LatLng coor = new LatLng(latitude, longitude);

			CustomCircle circle = new CustomCircle(coor, 50, list.getId());
			circle.setStrokeColor("#FF0000");
			circle.setFillColor("#FF0000");
			circle.setStrokeOpacity(0.7);
			circle.setFillOpacity(0.7);
			circleModel.addOverlay(circle);
		}
	}

	public void pesquisarEndereco() {
		GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(local + ", Brasília").setLanguage("pt")
				.getGeocoderRequest();
		GeocodeResponse geocoderResponse = Geocoder.geocode(geocoderRequest);
		List<GeocoderResult> results = geocoderResponse.getResults();
		listaLocais = new ArrayList<SelectItem>();
		for (GeocoderResult result : results) {

			Float latitude = result.getGeometry().getLocation().getLat().floatValue();
			Float longitude = result.getGeometry().getLocation().getLng().floatValue();

			String coor = latitude.toString() + "," + longitude.toString();
			listaLocais.add(new SelectItem(coor, result.getFormattedAddress()));

		}

		carregarCirulos();

	}

	public void save() {
		try {
			universalManager.save(ocorrencia);
			pesquisarEndereco();
			FacesMessagesUtil.addInfoMessage("Ocorrência ", "Salva " + ConstantesARQ.COM_SUCESSO);
		} catch (Exception e) {
			ExcecaoUtil.tratarExcecao(e);
		}
	}

	public void onPointSelect(PointSelectEvent event) {
		LatLng latlng = event.getLatLng();
		DecimalFormat df = new DecimalFormat("##.0000000");
		ocorrencia = new Ocorrencia();
		ocorrencia.setLatitude(df.format(latlng.getLat()));
		ocorrencia.setLongitude(df.format(latlng.getLng()));
	}

	public void onCircleSelect(OverlaySelectEvent event) {
		CustomCircle selectCircle = (CustomCircle) event.getOverlay();
		ocorrenciaVisu = (Ocorrencia) universalManager.get(Ocorrencia.class, selectCircle.getIdObject());
	}

	public String getLocal() {
		return local;
	}

	public void setLocal(String local) {
		this.local = local;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getCoordenadas() {
		return coordenadas;
	}

	public void setCoordenadas(String coordenadas) {
		this.coordenadas = coordenadas;
	}

	public List<SelectItem> getListaLocais() {
		return listaLocais;
	}

	public void setListaLocais(List<SelectItem> listaLocais) {
		this.listaLocais = listaLocais;
	}

	public Ocorrencia getOcorrencia() {
		return ocorrencia;
	}

	public void setOcorrencia(Ocorrencia ocorrencia) {
		this.ocorrencia = ocorrencia;
	}

	public UniversalManager getUniversalManager() {
		return universalManager;
	}

	public void setUniversalManager(UniversalManager universalManager) {
		this.universalManager = universalManager;
	}

	public MapModel getCircleModel() {
		return circleModel;
	}

	public void setCircleModel(MapModel circleModel) {
		this.circleModel = circleModel;
	}

	public Ocorrencia getOcorrenciaVisu() {
		return ocorrenciaVisu;
	}

	public void setOcorrenciaVisu(Ocorrencia ocorrenciaVisu) {
		this.ocorrenciaVisu = ocorrenciaVisu;
	}

}
